package com.fyyg.common.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.SFTPv3Client;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;


public class IMGUploadService {

    /**
     * 上传图片入口 --- 到图片服务器
     * savePath  
     * filename  ：图片名称
     * dirname   ：上传日期，（当前时间）
     * type      ：上传类型
     */
	
	/**
	 * 
	 * @param user			：服务器连接用户名
	 * @param pass			：服务连接密码
	 * @param host			：服务器IP
	 * @param savePath		:本地图片地址存放路径
	 * @param absolute_url	：服务器图片存放路径
	 * @return
	 */
    public static String imageUpload(String user,String pass ,String host,String savePath,String absolute_url,String dirname) {   
    		Connection  con = new Connection(host);   
    		try { 
    			con.connect();   
	            boolean isAuthed = con.authenticateWithPassword(user, pass);   
	            System.out.println("isAuthed===="+isAuthed);   
	            System.out.println("savePaht===="+savePath);   
    	              
	            try{
	   	    	 	SFTPv3Client sftpClient = new SFTPv3Client(con);   
	   	    	 	sftpClient.mkdir("/alidata/img1/pcupload/"+dirname+"/", 0755);    //远程新建目录   
	   	     	}catch(Exception e){
	   				System.out.println("文件夹已存在");
	   	     	}
    			
	            SCPClient scpClient = con.createSCPClient();   
	            scpClient.put(savePath, absolute_url+dirname+"/");
	               
	            Session session = con.openSession();   
	            session.execCommand("uname -a && date && uptime && who");   //远程执行命令   
	               
	            
	            //显示执行命令后的信息   
	            System.out.println("Here is some information about the remote host:");   
	            InputStream stdout = new StreamGobbler(session.getStdout());   
	            BufferedReader br = new BufferedReader(new InputStreamReader(stdout));   
	            while (true){   
	            	String line = br.readLine();   
	                if (line == null){
	                	break;   
	                }   
	                System.out.println(line);   
	            }   
	            System.out.println("ExitCode: " + session.getExitStatus());   
	            session.close();   
	            con.close();   
    			return "0";
    		}catch (Exception e) {   
    			e.printStackTrace();   
	        } 
		return "-1";
    }   
	
}
